package lwh.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import lwh.utils.DBOpenHelper;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class CheckProjectService {
	private DBOpenHelper dbOpenHelper;
	public CheckProjectService(Context context) {
		this.dbOpenHelper = new DBOpenHelper(context);
	}
	public List<HashMap<String,Object>> getInitList(String exm_pck_id,String prj_exm_id){
		List<HashMap<String,Object>> data =new ArrayList<HashMap<String,Object>>();
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select c.exm_item_id as exm_item_id,c.exm_item_name as exm_item_name,c.exm_item_crt_name as exm_item_crt_name,c.exm_item_type as exm_item_type,d.prj_exm_item_result as prj_exm_item_result from ((SELECT "
									+"a.exm_item_id,a.exm_item_name,b.exm_item_crt_name,a.exm_item_type "
									+"FROM exm_item a ,exm_item_criterion b WHERE	a.exm_item_id = b.exm_item_id AND "
									+"a.exm_pck_id = ?) c LEFT JOIN prj_exm_item  d ON c.exm_item_id = d.exm_item_id and d.prj_exm_id=?)",
				new String[]{exm_pck_id,prj_exm_id});
		while(cursor.moveToNext()){
			HashMap<String,Object> item=new HashMap<String, Object>();
    		item.put("exm_item_id", cursor.getString(cursor.getColumnIndex("exm_item_id")));
    		item.put("exm_item_name", cursor.getString(cursor.getColumnIndex("exm_item_name")));
    		item.put("exm_item_crt_name", cursor.getString(cursor.getColumnIndex("exm_item_crt_name")));
    		item.put("exm_item_type", cursor.getString(cursor.getColumnIndex("exm_item_type")));
    		if(cursor.getString(cursor.getColumnIndex("prj_exm_item_result"))==null||cursor.getString(cursor.getColumnIndex("prj_exm_item_result")).equals("null")){
    			item.put("prj_exm_item_result", "");
    		}else{
    			item.put("prj_exm_item_result", cursor.getString(cursor.getColumnIndex("prj_exm_item_result")));
    		}
    		
    		data.add(item);
		}
		cursor.close();
		db.close();
		return data;
	}
	
	public void closeDBHelper(){
		dbOpenHelper.close();
	}
}
